package cn.qdu.service;

import cn.qdu.dao.CommentDao;
import cn.qdu.dao.CommentDaoImpl;
import cn.qdu.dao.LikeDao;
import cn.qdu.dao.PostDao;
import cn.qdu.model.Comment;
import cn.qdu.model.Like;
import cn.qdu.model.Post;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PostService {
    private PostDao postDao = new PostDao();
    private CommentDao commentDao = new CommentDaoImpl();
    private LikeDao likeDao = new LikeDao();

    // 新增方法：带分页的获取日志
    public List<Post> getPostsForUser(String userId, String viewerId) {
        try {
            List<Post> posts = postDao.getPostsForUser(userId, viewerId);

            for (Post post : posts) {

                post.setCommentCount(commentDao.getCommentCountByPostId(post.getPostId()));

                // 设置点赞数
                post.setLikeCount(likeDao.getLikeCountByPostId(post.getPostId(), "like"));

                // 设置当前用户是否已点赞
                if (viewerId != null) {
                    List<Like> likes = likeDao.getLikesByPostId(post.getPostId());
                    boolean isLiked = likes.stream()
                            .anyMatch(like ->
                                    like.getUserId().equals(viewerId) &&
                                            "like".equals(like.getType()));
                    // 这里需要你在Post类中添加一个transient字段表示是否被当前用户点赞
                    post.setLikedByCurrentUser(isLiked);
                }
            }
            return posts;
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    public boolean createPost(Post post) {
        try {
            return postDao.createPost(post);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updatePost(Post post) {
        try {
            return postDao.updatePost(post);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deletePost(int postId, String userId) {
        try {
            return postDao.deletePost(postId, userId);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public Post getPostById(int postId, String viewerId) {
        try {
            Post post = postDao.getPostById(postId, viewerId);
            if (post != null) {
                post.setComments(getCommentsByPostId(postId));
                post.setCommentCount(commentDao.getCommentCountByPostId(postId));
            }
            return post;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 在PostService类中添加以下方法
    public List<Post> getPublicPosts(String userId) {
        try {
            return postDao.getPublicPosts(userId);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Post> getFriendsPosts(String userId) {
        try {
            return postDao.getFriendsPosts(userId);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Post> getUserPosts(String userId) {
        try {
            return postDao.getUserPosts(userId);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 添加评论相关方法
    public boolean addComment(Comment comment) {
        try {
            boolean success = commentDao.addComment(comment);
            if (success) {
                // 更新帖子的评论计数
                postDao.incrementCommentCount(comment.getPostId());
                return true;
            }
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    public int getCommentCountByPostId(int postId) {
        try {
            return postDao.getCommentCount(postId);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public List<Comment> getCommentsByPostId(int postId) {
        try {
            return commentDao.getCommentsByPostId(postId);
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    public boolean deleteComment(int commentId, String userId) {
        try {
            return commentDao.deleteComment(commentId, userId);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

}